import {
  KeywordMode
} from '../../modules/keyword_mode.js'
import {
  BookMode
} from '../../modules/book_mode.js'
import {
  paginationBev
} from '../behaviors/pagination.js'

const keywordmode = new KeywordMode()
const bookmode = new BookMode()
Component({
  /**
   * 组件的属性列表
   */
  // behaviors: [paginationBev],
  properties: {
    more: {
      type: String,
      observer: 'loadMore'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 移植到了../behaviors/pagination.js' 中
    dataArr: [],
    historyWords: [],
    hosWords: [],
    bookvalue: '',
    searching: false,
    loading: false,
    index: 1,
    loadingCenter: false
  },

  attached() {
    this.setData({
      historyWords: keywordmode.getHistory()
    })

    keywordmode.getHot(1).then(res => {
      res.map(item => {
        this.data.hosWords.push(item.title)
      })

      this.setData({
        hosWords: this.data.hosWords
      })
    })


  },


  /**
   * 组件的方法列表
   */
  methods: {
    onCancel() {
      this.triggerEvent('cancel', {}, {})
      this.setData({
        dataArr: []
      })
    },

    loadMore() {

      if (this.properties.more == 0) return
      if (this.data.loading) return
      this.setData({
        index: ++this.data.index,
        loading: true
      })
      console.log(this.data.loading)
      bookmode.getsearch(this.data.index).then(res => {
        this.setData({
          dataArr: this.data.dataArr.concat(res),
          loading: false
        })

        this.data.loading = false

      }, () => {
        this.data.loading = false
      })
    },









    onDelect() {
      this.setData({
        searching: false,
        bookvalue:''
      })
    },

    onConfirm(e) {
      const word = e.detail.value || e.detail.text
      this.setData({
        searching: true,
        loadingCenter: true
      })

      bookmode.getsearch(this.data.index).then(res => {
        this.setData({
          dataArr: res,
          bookvalue: word,
          loadingCenter: false
        })

        keywordmode.addToHistory(word)
      })
    }


  }
})